package com.fc.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.RequestMapping;
import com.fc.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.fc.entity.SysUser;
import com.fc.service.SysUserService;


import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.ModelAndView;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zffc
 * @since 2023-07-17
 */
@Controller
@RequestMapping("/sysUser" )
    public class SysUserController {


        @Autowired
        private SysUserService sysUserService;


        @GetMapping("/toIndex" )
        public ModelAndView index(){
           ModelAndView mv=new ModelAndView();
           //因为配置了自带的视图解析器，所以默认访问template目录
           mv.setViewName("/sysUser/list" );
           return mv;
        }

        @GetMapping("/toAdd" )
        public ModelAndView toAdd(){
           ModelAndView mv=new ModelAndView();
           mv.setViewName("/sysUser/form" );
           return mv;
        }

        @GetMapping("/toEdit/{id}" )
        public ModelAndView toEdit(@PathVariable String id){//路径参数
            ModelAndView mv=new ModelAndView();
            SysUser sysUser = sysUserService.getById(id);
            mv.setViewName("/sysUser/form" );
            mv.addObject("sysUser" ,sysUser);
            return mv;
        }


         /**
         *
         * @param sysUser
         * @param pageNumber
         * @param pageSize
         * @return
         */
         @GetMapping("/list" )
         @ResponseBody
         public Result<?> list(SysUser  sysUser,
                @RequestParam(name = "pageNumber" , defaultValue = "1" ) Integer pageNumber,
                @RequestParam(name = "pageSize" , defaultValue = "10" ) Integer pageSize){
           Page<SysUser> page=new Page(pageNumber,pageSize);
           return Result.OK(sysUserService.page(page));
        }

         /**
         * 添加或修改方法
         * @param sysUser
         * @return
         */
         @PostMapping("/addOrEdit" )
         @ResponseBody
         public Result<?> addOrEdit(@RequestBody SysUser sysUser){
            sysUserService.saveOrUpdate(sysUser);
            return Result.OK("ok" );
         }

         /**
         * 删除
         * @param id
         * @return
         */
         @GetMapping("/delete/{id}" )
         @ResponseBody
         public Result<?> delete(@PathVariable String id){
            sysUserService.removeById(id);
            return Result.OK("ok" );
         }


}

